import numpy as np
from PIL import Image
import cv2
import os


def get_boundary(mask, thicky=16):
	contour, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
	tempb = np.zeros_like(mask)
	tempb = cv2.drawContours(tempb, contour, -1, 255, thicky)
	return tempb

seg_img_path = "/media/wz209/a29353b7-1090-433f-b452-b4ce827adb17/JZT/segmentation/ISIC_2018/k_folder/mask"

img_list = sorted(os.listdir(seg_img_path))
mask_list = []
for p in img_list:
	templist = os.listdir(seg_img_path + "/" +p)
	for name in templist:
		mask_list.append(seg_img_path + "/" +p+"/"+name)

mask_list = sorted(mask_list)

print(len(mask_list))
# print(mask_list)
# exit(0)


# path = r"E:\Dataset\temp\skin\mask\f1\ISIC_0000000_segmentation.png"
# path1 = r"E:\Dataset\temp\skin\boundary\f1\ISIC_0000000_segmentation.png"
# path2 = r"E:\Dataset\temp\skin\boundary\f1\ISIC_0000000_boundary.png"


# img1 = Image.open(path1)
# img2 = Image.open(path2)
#
# if img1 == img2:
# 	print("相同 1")
#
# img1 = np.array(img1)
# img2 = np.array(img2)
#
# if img1.any() == img2.any():
# 	print("相同 2")
# if img1.all() == 0 or img1.all() == 255:
# 	print(" 0 and 255")

for path in mask_list:
	print(path)
	mask = Image.open(path)
	mask = np.array(mask)
	
	boundary = get_boundary(mask)
	# cv2.imshow("34", boundary)
	
	# mask2 = cv2.resize(boundary, (256, 256))
	# cv2.imshow("1233sdfsd434", mask2)
	# cv2.waitKey(0)
	
	path2 = path.replace("mask", "boundary")
	path2 = path2.replace("segmentation.png", "boundary.png")
	
	cv2.imwrite(path2, boundary)

